cap program drop tab_switchers_demographics
program define tab_switchers_demographics

	shell mkdir -p "$release/tables/switchers/"

	use "$sumstats_data_path/harmonized_switchers.dta", clear
  gen indnextonly = 1
  gen batch = 1
  
  append using "$sumstats_data_path/harmonized_switchers.dta", force
 	replace indnextonly = 0 if indnextonly == .
  replace batch = 2 if batch == .

  append using "$sumstats_data_path/harmonized_subs.dta", force
	replace switchers = 0 if switchers == .
  replace batch = 3 if batch ==.
  
  * Create some variables
  bysort contractnum batch year:gen contractnum_n_hh = _N
  gen contractnum_n_hh_l50 = contractnum_n_hh
  replace contractnum_n_hh_l50 = . if contractnum_n_hh > 50
  gen goldplat = (best_guess_metal == 4 | best_guess_metal == 5)
  replace goldplat = . if best_guess_metal == 0
  gen silver = (best_guess_metal == 3)	
  replace silver = . if best_guess_metal == 0
	
  * Drop the batch 1 folks who move to uninsurancedo not comply, then create columns
  drop if complier != 1 & batch == 1
	gen column = .
	replace column = 0 if sg_vs_indiv == 1
	replace column = 1 if switchers == 1 
	replace column = 2 if switchers == 1 & indnextonly == 1
	drop if column == . 
 
 tabout column using "$release/tables/switchers/counts.csv", style(csv) replace

        #delimit ;
	collapse 
	(mean) mean_single_mem_ind = single_mem
		(sd)   sd_single_mem_ind   = single_mem
		(p50)  p50_single_mem_ind  = single_mem
		(mean) mean_married_nokids_ind = married_nokids
		(sd)   sd_married_nokids_ind   = married_nokids
		(p50)  p50_married_nokids_ind  = married_nokids
		(mean) mean_unmarried_wkids_ind = unmarried_wkids
		(sd)   sd_unmarried_wkids_ind   = unmarried_wkids
		(p50)  p50_unmarried_wkids_ind  = unmarried_wkids
		(mean) mean_married_wkids_ind = married_wkids
		(sd)   sd_married_wkids_ind   = married_wkids
		(p50)  p50_married_wkids_ind  = married_wkids
		(mean) mean_ndeps_positive = ndeps_positive
		(sd)   sd_ndeps_positive   = ndeps_positive
		(p50)  p50_ndeps_positive  = ndeps_positive
		(mean) mean_sum_concurrent_risk = sum_concurrent_risk
		(sd)   sd_sum_concurrent_risk   = sum_concurrent_risk
		(p50)  p50_sum_concurrent_risk  = sum_concurrent_risk
		(mean) mean_age = age
		(sd)   sd_age   = age
		(p50)  p50_age  = age
		(mean) mean_income = best_guess_incomeoverfpl
		(sd)   sd_income   = best_guess_incomeoverfpl
		(p50)  p50_income  = best_guess_incomeoverfpl
		(mean) mean_over50_ind = over50
		(sd)   sd_over50_ind   = over50
		(p50)  p50_over50_ind  = over50
		(mean) mean_urban_ind = real_urban
		(sd)   sd_urban_ind   = real_urban
		(p50)  p50_urban_ind  = real_urban
    (mean) mean_goldplat = goldplat
    (sd)   sd_goldplat = goldplat
    (p50)  p50_goldplat = goldplat
    (mean) mean_silver = silver
    (sd)   sd_silver = silver
    (p50)  p50_silver = silver
    (mean) mean_group_size = contractnum_n_hh_l50
    (sd)   sd_group_size = contractnum_n_hh_l50
    (p50)  p50_group_size = contractnum_n_hh_l50
		,by(column)
	;
	#delimit cr
	reshape long mean_ sd_ p50_, i(column) j(dem_var, string)
	reshape wide mean_ sd_ p50_, i(dem_var) j(column)
	
	local dem_vars = "single_mem_ind married_nokids_ind unmarried_wkids_ind married_wkids_ind ndeps_positive sum_concurrent_risk income age over50_ind urban_ind group_size goldplat silver"
	word_orderer "`dem_vars'"
	drop_ind_sd "`dem_vars'" "0 1"
	
	export delimited using "$release/tables/switchers/demographics.csv", replace
end
